// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';

export async function currentUser(options?: { [key: string]: any }) {
  let userinfo = JSON.parse(sessionStorage.getItem('userinfo') || '{}');
  const defaultAvatar = 'https://img.xiaopiu.com/userImages/img9580018ca9178888.png';

  //校验 imgs 是否为空或无效
  let avatar = userinfo.imgs;
  if (!avatar || avatar === 'null' || avatar === 'undefined' || avatar === '') {
    avatar = defaultAvatar;
  }

  // 校验地址是否为合法图片URL
  const isLegalImg = await new Promise((resolve) => {
    const img = new Image();
    img.onload = () => resolve(true);
    img.onerror = () => resolve(false);
    img.src = avatar;
  });

  if (!isLegalImg) {
    avatar = defaultAvatar;
  }

  return {
    success: true,
    data: {
      name: userinfo.nickname || '默认用户',
      avatar: avatar, 
      userid: userinfo.id || '',    },
  }
}

/** 登录接口 */
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
  return request<API.LoginResult>('/pc/user/login', {
    method: 'POST',
    headers: {},
    data: body,
    ...(options || {}),
  });
}